package de.bbl.android.tools;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.bbl.android.classes.TableTeam;

public class XmlParseTableTeamTools {

	public static TableTeam[] XmlParseTableTeam(String xmlFilename) {
    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    	try {
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		Document dom = db.parse(new File(xmlFilename));
    		Element docElement = dom.getDocumentElement();
    		NodeList nl = docElement.getElementsByTagName("key");
    		int positionOfTeam = -1;
    		Node array = null;
    		
    		for (int i = 0; i < nl.getLength(); i++) {
    			String tempTextValue = nl.item(i).getTextContent().toString();
    			if (tempTextValue.equals("rankings")) {    				
    				positionOfTeam = i;
    				array = nl.item(i).getNextSibling();
    				while (array != null && !array.getNodeName().equals("array"))
    					array = array.getNextSibling();
    				
    				break;
    			}
    		}
    		if (positionOfTeam == -1)
    			return null;
    		
    		return XmlParseTeams(array);    		
    		
    	} catch (Exception e) {
        	Tools.AppendToErrorFile(e.getMessage());
            // e.printStackTrace();
    	}
		return null;
	}
	
	public static TableTeam[] XmlParseTeams(Node arrayNode) {
		
		ArrayList<TableTeam> stringList = new ArrayList<TableTeam>();

		NodeList nl = ((Element) arrayNode).getElementsByTagName("dict");
	    for (int i = 0; i < nl.getLength(); i++) {
	    	if (nl.item(i).getParentNode().getNodeName().equals("array")) {
	    		stringList.add(XmlParseSingleTeam(nl.item(i)));
	    	}
	    }
	    TableTeam[] retVal = new TableTeam[stringList.size()];
	    stringList.toArray(retVal);
		return retVal;
	}
	
	public static TableTeam XmlParseSingleTeam(Node matchNode) {
		String index = "";
		String id= "";
		String name= "";
		String played= "";
		String won= "";
		String lost= "";
		String pointsWon= "";
		String pointsLost= "";
		String winningStreak= "";
		
		// hier sind wir im ArrayNode
		int childNodeCount = matchNode.getChildNodes().getLength();
		
		for (int i = 0; i < childNodeCount; i++) {
			Node currentChildNode = matchNode.getChildNodes().item(i);
			
			if (checkKey(currentChildNode, "index")){
				index = getNextValue(currentChildNode);
			} else			
			if (checkKey(currentChildNode, "teamId")){
				id = getNextValue(currentChildNode);
			}  else		
			if (checkKey(currentChildNode, "teamName")){
				name = getNextValue(currentChildNode);
			} else 
			if (checkKey(currentChildNode, "played")){
				played = getNextValue(currentChildNode);
			} else 
			if (checkKey(currentChildNode, "won")){
				won = getNextValue(currentChildNode);
			}  
			if (checkKey(currentChildNode, "lost")){
				lost = getNextValue(currentChildNode);
			}  
			if (checkKey(currentChildNode, "pointsWon")){
				pointsWon = getNextValue(currentChildNode);
			}  
			if (checkKey(currentChildNode, "pointsLost")){
				pointsLost = getNextValue(currentChildNode);
			}  
			if (checkKey(currentChildNode, "winningStreak")){
				winningStreak = getNextValue(currentChildNode);
			}  
		}
		TableTeam retValTeam = new TableTeam(index, Integer.parseInt(id), name, played, won, lost, pointsWon, pointsLost, winningStreak);
		return retValTeam;
	}
	
	public static boolean checkKey(Node node,String match) {
		return node.getNodeName().equals("key") && node.getTextContent().equals(match);		
	}
	
	public static String getNextValue(Node currentNode) {
		String retval = "";
		currentNode = currentNode.getNextSibling();
		currentNode = currentNode.getNextSibling();
		if (currentNode.getNodeName().equals("integer") || currentNode.getNodeName().equals("string") || currentNode.getNodeName().equals("date")) {
			retval = currentNode.getTextContent();
		}				 
		return retval;
	}

}
